package com.jdew.thkj.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.jdew.thkj.R;

import java.util.ArrayList;
import java.util.List;

public class GridAdapter extends BaseAdapter{

	private LayoutInflater mInflater;
    private List<String> listUrls = new ArrayList<String>();
	private Context context;
    public GridAdapter(Context context,List list) {
        mInflater = LayoutInflater.from(context);
        this.context = context;
        this.listUrls = list;
    }

    public List<String> getList() {
		return listUrls;
	}

	public void setList(List<String> list) {
		this.listUrls = list;
		notifyDataSetChanged();
	}

	@Override
	public int getCount() {
        return listUrls.size();
    }

    @Override
	public Object getItem(int position) {
        return listUrls.get(position);
    }

    @Override
	public long getItemId(int position) {
        return position;
    }
    

	@Override
	public View getView(final int position, View convertView, ViewGroup arg2) {
		ViewHolder holder;
		if(convertView == null){  
			convertView = mInflater.inflate(R.layout.item,null);
			holder = new ViewHolder(convertView);
			holder.delect.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					listUrls.remove(position);
					setList(listUrls);
				}
			});
			 String path = listUrls.get(position);
			if (path.equals("paizhao")) {
				holder.pic.setImageResource(R.mipmap.find_add_img);
			} else {
				RequestOptions options = new RequestOptions().centerCrop();
				Glide.with(context).load(path).apply(options).into(holder.pic);
			}
			convertView.setTag(holder);  
		}else{  
			holder = (ViewHolder) convertView.getTag();  
        }
		return convertView;
	}
	
	 class ViewHolder {
		ImageView pic;
		ImageView delect;
		public ViewHolder(View v){
			pic = (ImageView) v.findViewById(R.id.pic);
			delect = (ImageView) v.findViewById(R.id.delect);
		}
	}
	
}